#! /bin/sh -e

### BEGIN INIT INFO
# Provides:          ti-sgx-ti33x-ddk-um.init
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start daemon at boot time
# Description:       Enable service provided by daemon.
### END INIT INFO

. /lib/lsb/init-functions

if [ ! -d /sys/bus/platform/devices/56000000.sgx ] ; then
  exit 0
fi

#@Copyright     Copyright (c) Imagination Technologies Ltd. All Rights Reserved
#@License       MIT
# The contents of this file are subject to the MIT license as set out below.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

# Auto-generated for omap_linux from build: sgxddk_MAIN@3699939
#

load_pvr()
{
	# Load the PVR Services module.
	#
	
	if ! /sbin/modprobe -q pvrsrvkm; then
		echo "Module pvrsrvkm failed to load. Retrying."
		if [ -z $depmod_has_been_run ]; then
			if [ -e /sbin/depmod ]; then
				echo "Running /sbin/depmod"
				/sbin/depmod && depmod_has_been_run=1
			fi
		fi
		if ! /sbin/modprobe -q pvrsrvkm; then return; fi
	fi

	# Run the PVR initialisation server
	if ! /usr/bin/pvrsrvctl --start --no-module; then return; fi

	# Load remaining modules.
	#
				
	if ! /sbin/modprobe -q bc_example ; then
		echo "Module bc_example failed to load. Retrying."
		if [ -z $depmod_has_been_run ]; then
			if [ -e /sbin/depmod ]; then
				echo "Running /sbin/depmod"
				/sbin/depmod && depmod_has_been_run=1
			fi
		fi
		if ! /sbin/modprobe -q bc_example ; then echo "Continuing to load PowerVR services"; bc_example_failed=1; fi
	fi
	
	# Delete the device for bc_example and recreate with the
	# correct major number.
	#
	if [ -z $bc_example_failed ]; then
		bcd_maj=`grep "bc_example$" /proc/devices | cut -b1,2,3`
		/bin/rm -f /dev/bc_example
		/bin/mknod /dev/bc_example c $bcd_maj 0
		/bin/chmod 666 /dev/bc_example
	fi

	echo "Loaded PowerVR consumer services."
	return 0;
}

unload_pvr()
{
	# Unload modules.  This won't pick up cases where a further module
	# has been added which has a dependency on one of these modules.
	#
		if rmmod bc_example; then :; else echo "Warning: Could not unload bc_example"; fi

	# Unload the PVR Services module.
	#
	if rmmod pvrsrvkm; then :; else return 1; fi
	

	echo "Unloaded PowerVR consumer services."
	return 0;
}

# Deal with the type of invocation we get.
#
case "$1" in
"start")
	load_pvr
	;;
stop)
	if ! unload_pvr; then
		echo "Couldn't unload modules" >&2;
	fi
	;;
reload|restart)
	if unload_pvr; then
		load_pvr
	else
		echo "Couldn't unload modules" >&2;
	fi
	;;
*)
	echo "$0: unknown argument $1." >&2;
	;;
esac
